import org.junit.jupiter.api.Test;

import java.util.*;


/**
 * 先排序，然后先固定两个数，在剩下的里面找两个数的和等于相反数，去重
 */
public class _49_字母异位词分组_哈希表法 {

    @Test
    public void test() {
        //       String [] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        String[] strs = {"ddddddddddg", "dgggggggggg"};
        System.out.println(groupAnagrams(strs));
    }

        public List<List<String>> groupAnagrams(String[] strs) {
            int len = strs.length;
            String[] tempstrs = new String[len];
            Map<String, List<String>> map = new HashMap<>();

            for(int i = 0; i < len; i++){
                char[] cs = strs[i].toCharArray();
                Arrays.sort(cs);
                String key = String.valueOf(cs);

                if (!map.containsKey(key))
                    map.put(key, new ArrayList<>());

                map.get(key).add(strs[i]);
            }
            return new ArrayList<>(map.values());
        }
    }


